package com.fancy.learncenter.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.TouchDelegate;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.CartoonMediaBean;
import com.fancy.learncenter.utils.LogUtil;

import java.util.List;

/**
 * Created by shiweiwei on 2017/12/25.
 */

public class GridViewAdapter extends BaseAdapter {


    private Context context;
    private List<CartoonMediaBean> strList;
    private int hidePosition = AdapterView.INVALID_POSITION;

    public GridViewAdapter(Context context, List<CartoonMediaBean> strList) {
        this.context = context;
        this.strList = strList;
    }

    public List<CartoonMediaBean> getStrList() {
        return strList;
    }

    @Override
    public int getCount() {
        return strList.size();
    }

    @Override
    public CartoonMediaBean getItem(int position) {
        return strList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.cartoon_puzzle_item, null);
            viewHolder.simpleDraweeView = (SimpleDraweeView) convertView.findViewById(R.id.simpleDraweeView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        //hide时隐藏Text
        if (position != hidePosition) {
            viewHolder.simpleDraweeView.setVisibility(View.VISIBLE);
            viewHolder.simpleDraweeView.setImageURI(strList.get(position).getImgUrl());
        } else {
            viewHolder.simpleDraweeView.setImageURI("");
        }

        convertView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return false;
            }
        });
//        viewHolder.simpleDraweeView.setVisibility(View.VISIBLE);
//        viewHolder.simpleDraweeView.setImageURI(strList.get(position).getImgUrl());

        return convertView;
    }



    public void hideView(int pos) {
        hidePosition = pos;
        notifyDataSetChanged();
    }

    public void showHideView() {
        hidePosition = AdapterView.INVALID_POSITION;
        notifyDataSetChanged();
    }

    public void removeView(int pos) {
        strList.remove(pos);
        notifyDataSetChanged();
    }

    //更新拖动时的gridView
    public void swapView(int draggedPos, int destPos) {
//        //从前向后拖动，其他item依次前移
//        if(draggedPos < destPos) {
//            strList.add(destPos+1, getItem(draggedPos));
//            strList.remove(draggedPos);
//        }
//        //从后向前拖动，其他item依次后移
//        else if(draggedPos > destPos) {
//            strList.add(destPos, getItem(draggedPos));
//            strList.remove(draggedPos+1);
//        }

        CartoonMediaBean draggedBean = strList.get(draggedPos);
        CartoonMediaBean destBean = strList.get(destPos);
        strList.set(draggedPos, destBean);
        strList.set(destPos, draggedBean);

        hidePosition = destPos;
        notifyDataSetChanged();
    }

    class ViewHolder {
        SimpleDraweeView simpleDraweeView;
    }
}
